#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
#include <strings.h>

class InetAddr
{
public:
    InetAddr()
    {}

    InetAddr(const struct sockaddr_in &addr)
    {
        _ip = inet_ntoa(addr.sin_addr);
        _port = ntohs(addr.sin_port);
    }

    InetAddr(std::string ip,uint16_t port):_ip(ip),_port(port)
    {
        bzero(&addr,sizeof(addr));
        addr.sin_family=AF_INET;
        addr.sin_addr.s_addr=inet_addr(ip.c_str());
        addr.sin_port=htons(port);
    }

    void operator=(const struct sockaddr_in &addr)
    {
        _ip=inet_ntoa(addr.sin_addr);
        _port=ntohs(addr.sin_port);
    }

    std::string ip()
    {
        return _ip;
    }

    uint16_t port()
    {
        return _port;
    }

private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in addr;
};